package com.wudl.avro.testtool;

import com.wudl.avro.bean.User;
import org.apache.avro.file.DataFileReader;
import org.apache.avro.file.DataFileWriter;
import org.apache.avro.file.SeekableByteArrayInput;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.DatumWriter;
import org.apache.avro.specific.SpecificDatumReader;
import org.apache.avro.specific.SpecificDatumWriter;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ：wudl
 * @date ：Created in 2022-01-11 23:40
 * @description：
 * @modified By：
 * @version: 1.0
 */

public class UserTest {

    public static void main(String[] args) throws IOException {
        // 序列化
        // SerializingUser();
        // 反序列化
        // DeserializingUser();
        // Byte 序列化
        byte[] bytes = byteSerializingUser();
        // 将数据反序列化
        byteDeserializingUser(bytes);

    }

    /**
     * User 类的序列化
     */
    private static void SerializingUser() throws IOException {
        List<User> list = new ArrayList<>();
        User u = new User();
        u.setId(1);
        u.setName("flink");
        u.setAdress("深圳");
        u.setAge(21);
        list.add(u);

        User u01 = new User();
        u01.setId(2);
        u01.setName("flink02");
        u01.setAdress("深圳-02");
        u01.setAge(20);
        list.add(u01);

//        DatumWriter<User> userDatumWriter = new SpecificDatumWriter<User>(User.class);
//        DataFileWriter<User> dataFileWriter = new DataFileWriter<User>(userDatumWriter);
//        dataFileWriter.create(u.getSchema(), new File("D:\\tmp\\users-QQ.avro"));
//        dataFileWriter.append(u);
//        dataFileWriter.append(u01);
//        System.out.println(dataFileWriter);
//        dataFileWriter.close();

        DatumWriter<User> userDatumWriter = new SpecificDatumWriter<User>(User.class);
        DataFileWriter<User> dataFileWriter = new DataFileWriter<User>(userDatumWriter);
        dataFileWriter.create(u.getSchema(), new File("D:\\tmp\\users-QQ.avro"));
        // List 数组序列化
        for (User user : list) {
            dataFileWriter.append(user);
        }
        System.out.println(dataFileWriter);
        dataFileWriter.close();
    }

    /**
     * 反序列化方式
     */
    private static void DeserializingUser() throws IOException {
        DatumReader<User> userDatumReader = new SpecificDatumReader<User>(User.class);
        DataFileReader<User> dataFileReader = new DataFileReader<User>(new File("D:\\tmp\\users-QQ.avro"), userDatumReader);
        User user = null;
        while (dataFileReader.hasNext()) {
            user = dataFileReader.next(user);
            System.out.println(user);
        }
    }


    /**
     * 序列化Byte 数组类型
     *
     * @return
     * @throws IOException
     */
    private static byte[] byteSerializingUser() throws IOException {

        List<User> list = new ArrayList<>();
        User u = new User();
        u.setId(1);
        u.setName("flink");
        u.setAdress("深圳");
        u.setAge(21);
        list.add(u);

        User u01 = new User();
        u01.setId(2);
        u01.setName("flink02");
        u01.setAdress("深圳-02");
        u01.setAge(20);
        list.add(u01);
        ByteArrayOutputStream byteUser = new ByteArrayOutputStream();
        DatumWriter<User> userDatumWriter = new SpecificDatumWriter<User>(User.class);
        DataFileWriter<User> dataFileWriter = new DataFileWriter<User>(userDatumWriter);
        dataFileWriter.create(list.get(0).getSchema(), byteUser);
        for (User user : list) {
            dataFileWriter.append(user);
        }
        dataFileWriter.close();
        System.out.println(byteUser.toString());
        return byteUser.toByteArray();
    }


    /**
     * 反序列化
     *
     * @param usersByteArray
     * @throws IOException
     */
    public static void byteDeserializingUser(byte[] usersByteArray) throws IOException {
        SeekableByteArrayInput sbai = new SeekableByteArrayInput(usersByteArray);
        DatumReader<User> userDatumReader = new SpecificDatumReader<User>(User.class);
        DataFileReader<User> dataFileReader = new DataFileReader<User>(sbai, userDatumReader);
        System.out.println("----------------deserialzeAvroFromByteArray-------------------");
        User readUser = null;
        while (dataFileReader.hasNext()) {
            readUser = dataFileReader.next(readUser);
            System.out.println(readUser);
        }
    }

}
